//
//  BroadcastScaleViewController.m
//  AILinkBleSDKSourceCode
//
//  Created by iot_user on 2020/9/12.
//  Copyright © 2020 IOT. All rights reserved.
//

#import "BroadcastScaleViewController.h"
#import <AILinkBleSDK/ELBroadcastScaleBleManager.h>
#import "Masonry.h"

@interface BroadcastScaleViewController ()<BroadcastScaleBleDelegate>
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *statusLbl;


@end

@implementation BroadcastScaleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [ELBroadcastScaleBleManager shareManager].broadcastScaleBleDelegate = self;
    [[ELBroadcastScaleBleManager shareManager] startScan];
    [self setupUI];
}


-(void)broadcastScaleBleDataModel:(ELBroadcastScaleDataModel *)model{
    switch (model.testStatus) {
        case BroadcastScaleTestStatusWeightTesting:
        {
            self.statusLbl.text = @"Weight testing";
        }
            break;
        case BroadcastScaleTestStatusADCTesting: {
            self.statusLbl.text = @"Impedance testing";
            break;
        }
        case BroadcastScaleTestStatusADCTestSuccess: {
            self.statusLbl.text = @"Impedance test success";
            break;
        }
        case BroadcastScaleTestStatusADCTestFailed: {
            self.statusLbl.text = @"Impedance test failed";
            break;
        }
        case BroadcastScaleTestStatusTestEnd: {
            self.statusLbl.text = @"Test end";
            
            break;
        }
    }
    
    NSString *testData = [NSString stringWithFormat:@"MAC:%@\ncid = %d--vid=%d--pid=%d\nWeight = %@%@\nADC = %d",model.mac,model.cid-65535,model.vid,model.pid,model.weight,AiLinkBleWeightUnitDic[@(model.weightUnit)],model.adc];
    [self addLog:testData];
}
-(void)broadcastScaleBleUpdateState:(ELBluetoothState)state{
    if (state == ELBluetoothStateAvailable) {
        self.statusLbl.text = @"Connecting";
    }else if (state == ELBluetoothStateUnavailable){
        self.statusLbl.text = @"Bluetooth is disconnected";
    }

}

-(void)addLog:(NSString *)log{
    self.textView.text = [NSString stringWithFormat:@"%@\n%@",log,self.textView.text];
}

-(void)setupUI{
    self.textView = [[UITextView alloc] init];
    self.textView.backgroundColor = [UIColor blackColor];
    self.textView.text = @"Log";
    self.textView.textColor = [UIColor redColor];
    [self.view addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.bottom.mas_equalTo(-10);
        make.height.equalTo(self.view).multipliedBy(0.7);
    }];
    
    //
    self.statusLbl = [[UILabel alloc] init];
    self.statusLbl.text = @"Connecting";
    self.statusLbl.adjustsFontSizeToFitWidth = YES;
    self.statusLbl.font = [UIFont boldSystemFontOfSize:30];
    self.statusLbl.textColor = [UIColor redColor];
    self.statusLbl.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.statusLbl];
    [self.statusLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.right.equalTo(self.textView);
        make.bottom.equalTo(self.textView.mas_top).mas_offset(-20);
    }];
}
-(void)dealloc{
    
    [[ELBroadcastScaleBleManager shareManager] stopScan];
}

@end
